<?php
/*
 * @Author: zwx
 * @Date: 2024-07-13 22:01:29
 * @LastEditors: zwx
 * @LastEditTime: 2024-07-29 11:19:03
 * @Description: 
 */

namespace app\apiadmin\service\course;

use app\apiadmin\model\course\CourseModel;

use app\common\utils\Res;
use think\facade\Db;
use think\facade\Log;
use Predis\Client;

class CourseService
{
    public static $pre="siyu_course_";
    /**
     * @author: zwx
     * @Description: 根据条件查询数据列表
     * @param {*} $param
     * @Date: 2024-05-12 14:50:39
     */
    public static function getCourseData($param, $uid)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;


        $map = [];
        $map["y.del"] = 0;

        if (isset($param["name"]) && !empty($param["name"])) {
            $map[] = ["y.name", 'like', '%' . $param["name"] . '%'];
        }

        $findName = "y.*";

        //查询分页数据和条数
        $result = CourseModel::pageMap($map, $findName, $offset, $limit);
        $total = CourseModel::pageMapCount($map);

        $redis=self::Redis();
        //组合信息
        foreach ($result as $k => $v) {
            if ($v['teacher_id'] == 1) {
                $result[$k]['teacher'] = '李洁老师';
            }
            $result[$k]['fee']= $result[$k]['fee']/100;
            $result[$k]['sale']= $result[$k]['sale']/100;
            // $result[$k]['update_fee_pay_url'] = '';
            // $key=md5(self::$pre.$v['id']);
            // $value = $redis->get($key);
            // if($value){
                // $result[$k]['update_fee_pay_url'] = env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $v['id'] . '&tutorId=' . $uid . '&key='.$key;
            // }
            $result[$k]['pay_url'] = env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $v['id'] . '&tutorId=' . $uid . '&';
            $result[$k]['pay_order'] = env('Live.live_host').'/pages/animate/myorder/myorder?from=3&';
        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }

    //修改价格生成改价支付链接
    public static function updateCourseFee($param,$uid){
        if(!isset($param['type']) || empty($param['type'])){
            return Res::error("缺少必要参数生成改价链接类型");
        }
        $type=$param['type'];
        switch ($type){
            case 'course':
                return self::getCourseFeeLink($param,$uid);
            case 'deposit':
                return self::getDepositFeeLink($param,$uid);
            default:
                $link='';
                break;
        }
        return Res::ok(['link'=>$link]);
    }

    public static function getCourseFeeLink($param,$uid){
        if(!isset($param['id']) || empty($param['id'])){
            return Res::error("缺少必要参数课程id");
        }
        if(!isset($param['fee']) || empty($param['fee'])){
            return Res::error("缺少必要参数课程改价价格");
        }
        $siyu_course_id=$param['id'];
        $update_fee=bcmul($param['fee'],100);
        $info = Db::name('siyu_course')->where(['id'=>$siyu_course_id])->find();
        if(!$info){
            return Res::error("课程不存在");
        }
        
        $redis = self::Redis();

        $key=md5(self::$pre.$siyu_course_id.time());

        $value=json_encode(['type'=>$param['type'],'siyu_course_id'=>$siyu_course_id,'fee'=>$update_fee]);
        
        $redis->set($key,$value);
        $redis->expire($key,300);
        //记录操作日志
        $log_data=[
            'siyu_course_id'=>$siyu_course_id,
            'uid'=>$uid, 
            'fee'=>$info['fee'],
            'update_fee'=>$update_fee
        ];
        Db::name('siyu_course_opt_log')->insert($log_data);
        $link = env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $siyu_course_id . '&tutorId=' . $uid . '&key='.$key;
        return  Res::ok(['link'=>$link]);
    }
    public static function getDepositFeeLink($param,$uid){
        if(!isset($param['id']) || empty($param['id'])){
            return Res::error("缺少必要参数定金支付id");
        }
        if(!isset($param['order_fee']) || empty($param['order_fee'])){
            return Res::error("缺少必要参数课程尾款应付价格");
        }
        $deposit_id = $param['id'];
        $order_fee = bcmul($param['order_fee'],100);
        $info = Db::name('siyu_deposit')->where(['id'=>$deposit_id])->find();
        if(!$info){
            return Res::error("定金不存在");
        }
        if($info['state']!=1){
            return Res::error("定金非已支付状态");
        }
        if($info['siyu_order_id']){
            return Res::error("定金尾款已支付");
        }
        $c_info = Db::name('siyu_course')->where(['id'=>$info['siyu_course_id']])->find();
        if(!$c_info){
            return Res::error("课程不存在");
        }
        if($order_fee>bcsub($c_info['fee'],$info['actual_payment_fee'])){
            return Res::error("尾款应付金额不能大于课程价格-已支付定金");
        }

        $redis = self::Redis();

        $key=md5(self::$pre.$deposit_id.time());

        $value=json_encode(['type'=>$param['type'],'siyu_deposit_id'=>$deposit_id,'siyu_course_id'=>$info['siyu_course_id'],'fee'=>$order_fee]);
        $redis->set($key,$value);
        $redis->expire($key,300);
        //记录操作日志
        $log_data=[
            'siyu_deposit_id'=>$deposit_id,
            'uid'=>$uid, 
            'fee'=>$c_info['fee'],
            'update_fee'=>$order_fee
        ];
        Db::name('siyu_course_opt_log')->insert($log_data);
        $link = env('Live.live_host').'/pages/animate/myorder/gwpayorder?from=1&courseid=' . $info['siyu_course_id'] . '&tutorId=' . $uid . '&key='.$key;
        return  Res::ok(['link'=>$link]);
    }

    public static function Redis(){
        $redis = new Client([
            'scheme' => 'tcp',
            'host' => env('redis.host'),
            'port' => env('redis.port'),
            'password' => env('redis.password'),
            "username" => env('redis.username'),
    
        ]);
        return $redis;
    }
   

}